import { DEF } from "../definition/consts";
import { Player } from "../definition/player";
import { Room } from "../definition/room";

export class PlayerManager {

  private _room: Room;
  private _players: Array<Player>;

  constructor(room: Room) {
    this._room = room;
    this._players = new Array();
  }

  public addPlayer(uid: number, nickname: string, sid: string, isRobot: boolean = false) {
    if (!this.isPlayerInRoom(uid)) {
      const player: Player = new Player(uid, sid, nickname, isRobot);
      this._players.push(player);
      return player;
    }
    return null;
  }

  public removePlayer(uid: number) {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].uid === uid) {
        this._players.splice(i, 1);
        return;
      }
    }
  }

  public getPlayerCount() {
    return this._players.length;
  }

  public getAllPlayers() {
    return this._players;
  }

  public getAllPlayersUid() {
    return this._players.map(player => player.uid);
  }

  public getAllPlayersUidsid() {
    return this._players.map(player => { return { uid: player.uid, sid: player.sid } });
  }

  public getAllPlayersUidsidExceptUid(uid: number) {
    const players = this._players.filter(player => player.uid !== uid);
    return players.map(player => { return { uid: player.uid, sid: player.sid } })
  }

  public getPlayerByUid(uid: number) {
    return this._players.find(player => player.uid === uid);
  }

  public getPlayerStateByUid(uid: number) {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].uid === uid) {
        return this._players[i].state
      }
    }
    return DEF.PLAYER.STATE.UNKNOW;
  }

  public updatePlayerStateByUid(uid: number, state: number) {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].uid === uid) {
        this._players[i].state = state;
        return;
      }
    }
  }

  public updateAllPlayerState(state: number) {
    for (let i = 0; i < this._players.length; i++) {
      this._players[i].state = state;
    }
  }

  public isPlayerInRoom(uid: number) {
    return !!this._players.find(player => player.uid === uid);
  }

  public isAllPlayerReady() {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].state !== DEF.PLAYER.STATE.READY) {
        return false;
      }
    }
    return true;
  }

  public isAllPlayerLoaded() {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].state !== DEF.PLAYER.STATE.LOADED) {
        return false;
      }
    }
    return true;
  }

  public isPlayerPlaying(uid: number) {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].uid === uid) {
        return this._players[i].state === DEF.PLAYER.STATE.PLAYING;
      }
    }
    return false;
  }

  public isPlayerOffline(uid: number) {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].uid === uid) {
        return this._players[i].state === DEF.PLAYER.STATE.OFFLINE;
      }
    }
    return false;
  }

  public isAllPlayerOffline() {
    for (let i = 0; i < this._players.length; i++) {
      if (this._players[i].state !== DEF.PLAYER.STATE.OFFLINE) {
        return false;
      }
    }
    return true;
  }

}
